/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package interfaz;

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.JFileChooser.*;
import javax.swing.filechooser.FileFilter.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import persistencia.Serializacion;

/**
 *
 * @author Yanire
 */
public class PopUp extends JDialog{
    static final public int alto = 400;
    static final public int ancho = 300;
    static final private int JUEGO = 2;
    private Menu2 menu2;
    private LaVentana owner=null;
    

    public PopUp(LaVentana owner, Boolean modal){
        super(owner, modal);
        setSize(ancho, alto);
        setTitle("Que desea hacer:");
        setLayout(null);
        this.setResizable(false);
        setLocationRelativeTo(null);
        //this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

        this.owner=owner;
        menu2 = new Menu2();
        menu2.setBounds(0, 0, ancho, alto);
        menu2.setVisible(true);

        add(menu2);

        menu2.getRetornar().addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                menu2.setVisible(false);
                PopUp.this.dispose();
            }
        });

        menu2.getAbrir().addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                menu2.setVisible(false);

                JFileChooser chooser= new JFileChooser("./savedGames");
                FileNameExtensionFilter filter= new FileNameExtensionFilter("*.xml & *.txt", "xml", "txt");

                chooser.setFileFilter(filter);
                int returnVal = chooser.showOpenDialog(new JDialog());
                if(returnVal == JFileChooser.APPROVE_OPTION) {
                    PopUp.this.owner.getJuego().deserializar(chooser.getSelectedFile().getPath());
                    menu2.setVisible(false);
                    PopUp.this.dispose();                                        
                    PopUp.this.owner.tiempo=JUEGO;
                    PopUp.this.owner.repaint();
                }
                 else
                     menu2.setVisible(true);


            }
        });

        menu2.getGuardar().addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                

                Serializacion archivo = new Serializacion();
                JFileChooser chooser = new JFileChooser("./savedGames");
                FileNameExtensionFilter  filter = new FileNameExtensionFilter("*.xml & *.txt", "xml", "txt");
                chooser.setFileFilter(filter);
                int returnVal = chooser.showSaveDialog(new JDialog());
                if(returnVal == JFileChooser.APPROVE_OPTION)
                   archivo.Serializacion(PopUp.this.owner.getJuego().getGestor(),chooser.getSelectedFile().getPath());
                //menu2.setVisible(false);
                //PopUp.this.dispose();
            }
        });
      
        menu2.getSalir().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });

        setVisible(true);

        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        
    }

    public void paintComponent(Graphics g){
        g.drawImage(Imagenes.fondo_matrix,0,0,owner);       
    }

}
